package sui.utils.file;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


public class FileUtils {
	public static void main(String[] args) {
		httpDownload("http://10.100.141.29/uic/idcard/3e4462a201834142abafcc628b534974", "E:\\a.jpg ");
	}
	
	/** http下载 */
	public static boolean httpDownload(String httpUrl, String saveFile) {
		// 下载网络文件
		int bytesum = 0;
		int byteread = 0;
		
		URL url = null;
		try {
			url = new URL(httpUrl);
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return false;
		}
		
		try {
			URLConnection conn = url.openConnection();
			
			HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection(); 
			URL absUrl = httpConnection.getURL();

			System.out.println(absUrl);
			
			InputStream inStream = conn.getInputStream();
			@SuppressWarnings("resource")
			FileOutputStream fs = new FileOutputStream(saveFile);
			
			byte[] buffer = new byte[1204];
			while ((byteread = inStream.read(buffer)) != -1) {
				bytesum += byteread;
				System.out.println(bytesum);
				fs.write(buffer, 0, byteread);
			}
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
}
